// 
// Decompiled by Procyon v0.5.30
// 

package com.baturu.vin.benz.dto.footnote;

import com.baturu.vin.benz.dto.BenzVinInfoDTO;
import com.baturu.vin.benz.service.impl.util.BenzStringUtil;
import org.apache.commons.lang3.StringUtils;

public abstract class FootnoteRev8 extends Footnote
{
    protected String whcForFootnote;

    protected boolean isPaintCodeFiltered(String paintCode, String codeString) {
        int index = 0;
        if ((index = codeString.indexOf(paintCode)) != -1) {
            if (index == 0) {
                return false;
            }
            --index;
            String precedingChar = codeString.substring(index, index + 1);
            return precedingChar.equals("-");
        }
        return true;
    }
    
    private String checkFnText(String serialNumber, String abbr) {
        String tmpSerial = this.checkFNForWHC(serialNumber);
        if (tmpSerial.length() == 6 && BenzStringUtil.isAllDigits(tmpSerial, 0, tmpSerial.length())) {
            serialNumber = "0" + tmpSerial;
        }
        else if (tmpSerial.length() == 8) {
            if (abbr.equals("#AM") || abbr.equals("#BM")) {
                if ((tmpSerial.charAt(1) == '*' && BenzStringUtil.isAllDigits(tmpSerial, 2, tmpSerial.length())) || BenzStringUtil.isAllDigits(tmpSerial, 1, tmpSerial.length())) {
                    serialNumber = tmpSerial;
                }
                else {
                    serialNumber = null;
                }
            }
            else if (tmpSerial.charAt(1) == ' ' && (Character.isDigit(tmpSerial.charAt(0)) || Character.isLetter(tmpSerial.charAt(0))) && BenzStringUtil.isAllDigits(tmpSerial, 2, tmpSerial.length())) {
                serialNumber = tmpSerial.substring(0, 1) + tmpSerial.substring(2);
            }
            else if ((Character.isDigit(tmpSerial.charAt(0)) || Character.isLetter(tmpSerial.charAt(0))) && (Character.isDigit(tmpSerial.charAt(1)) || Character.isLetter(tmpSerial.charAt(1))) && BenzStringUtil.isAllDigits(tmpSerial, 2, tmpSerial.length())) {
                serialNumber = tmpSerial;
            }
            else {
                serialNumber = null;
            }
        }
        else {
            serialNumber = null;
        }
        return serialNumber;
    }
    
    @Override
    public int isValid(FootnoteList fnList, BenzVinInfoDTO benzVinInfoDTO, int statusCKD) {
        if (this.hasBeenChecked) {
            return this.lastValidReturnValue;
        }
        String cab = "";
        String chassis = null;
        String engine = "";
        String serialNumber = null;
        String ftntSerialNumber = null;
        String abbr = null;
        String whc = null;
        int returnValue = 0;
        if (this.getText() == null) {
            return this.setValid(0);
        }
        if (!this.isDoIdentfilter(this.getAbbr())) {
            return this.setValid(0);
        }
        if (benzVinInfoDTO != null) {
            cab = benzVinInfoDTO.getBody();
            if (cab != null) {
                cab = cab.trim();
            }
            engine = benzVinInfoDTO.getEngine();
            if (engine != null) {
                engine = engine.trim();
            }
            chassis = benzVinInfoDTO.getChassIdent();
            whc = benzVinInfoDTO.getWhc();
        }
        abbr = this.getAbbr();
        int valid = 0;
        if (abbr != null && abbr.length() > 2 && abbr.charAt(0) == '#') {
            serialNumber = this.checkFnText(serialNumber, abbr);
            if (serialNumber != null) {
                this.isSerial = true;
                if (abbr.equals("#AF") && chassis != null) {
                    ftntSerialNumber = this.getUnformattedSerialNum();
                    valid = this.processChassis(fnList, chassis, ftntSerialNumber, whc, abbr);
                }
                else if (abbr.equals("#AM") && engine != null) {
                    valid = this.processMotor(engine, serialNumber, abbr);
                }
                else if (abbr.equals("#AA") && cab != null) {
                    valid = this.processCab(cab, serialNumber, benzVinInfoDTO, abbr);
                }
                else if (abbr.equals("#BF") && chassis != null) {
                    ftntSerialNumber = this.getUnformattedSerialNum();
                    valid = this.processChassis(fnList, chassis, ftntSerialNumber, whc, abbr);
                }
                else if (abbr.equals("#BM") && engine != null) {
                    valid = this.processMotor(engine, serialNumber, abbr);
                }
                else if (abbr.equals("#BA") && cab != null) {
                    valid = this.processCab(cab, serialNumber, benzVinInfoDTO, abbr);
                }
            }
        }
        returnValue = this.setValid(valid);
        return this.setValid(returnValue);
    }
    
    private String getUnformattedSerialNum() {
        String text = this.getText().trim();
        String unformatted;
        if (text.length() > 8) {
            unformatted = text.substring(0, 8).trim();
        }
        else {
            unformatted = text;
        }
        return unformatted;
    }
    
    protected String checkFNForWHC(String fnSerialIdent) {
        String whcFNFormat = "(WHC: ";
        if(StringUtils.isBlank(fnSerialIdent)) {
            return "";
        }
        int whcIndex = fnSerialIdent.indexOf(whcFNFormat);
        if (whcIndex != -1) {
            int endParenIndex = fnSerialIdent.indexOf(")", whcIndex + 1);
            if (endParenIndex != -1) {
                String tempWHC = fnSerialIdent.substring(whcIndex + whcFNFormat.length(), endParenIndex);
                if (tempWHC.length() == 3) {
                    this.whcForFootnote = tempWHC;
                    fnSerialIdent = fnSerialIdent.substring(0, whcIndex);
                }
            }
        }
        return fnSerialIdent.trim();
    }
    
    private String checkNewFTNoteFormat(String fnSerialIdent) {
        String formattedIdent = "";
        fnSerialIdent = this.checkFNForWHC(fnSerialIdent);
        if (fnSerialIdent.length() == 8 && !Character.isSpaceChar(fnSerialIdent.charAt(0)) && Character.isSpaceChar(fnSerialIdent.charAt(1)) && this.isSixNums(fnSerialIdent.substring(2, 8))) {
            formattedIdent = fnSerialIdent.substring(0, 1) + fnSerialIdent.substring(2, 8);
        }
        else if (fnSerialIdent.length() == 8 && !Character.isSpaceChar(fnSerialIdent.charAt(0)) && !Character.isSpaceChar(fnSerialIdent.charAt(1)) && this.isSixNums(fnSerialIdent.substring(2, 8))) {
            formattedIdent = fnSerialIdent;
        }
        else if (fnSerialIdent.length() == 7 && this.isSixNums(fnSerialIdent.substring(1, 7))) {
            formattedIdent = fnSerialIdent;
        }
        else if (fnSerialIdent.length() == 6 && this.isSixNums(fnSerialIdent.substring(0, 6))) {
            formattedIdent = fnSerialIdent;
        }
        else if (fnSerialIdent.length() == 8 && Character.isSpaceChar(fnSerialIdent.charAt(0)) && Character.isSpaceChar(fnSerialIdent.charAt(1)) && this.isSixNums(fnSerialIdent.substring(2, 8))) {
            formattedIdent = fnSerialIdent.trim();
        }
        return formattedIdent;
    }
    
    private String getPlantCodeGroup(String plantCodes, char cPlantCode) {
        int j;
        String tmpPlantCodes;
        String tmpPlantCodes2;
        for (j = 0, tmpPlantCodes = plantCodes; (j = tmpPlantCodes.lastIndexOf(",")) != -1; tmpPlantCodes = tmpPlantCodes.substring(0, j)) {
            tmpPlantCodes2 = tmpPlantCodes.substring(j + 1);
            if (tmpPlantCodes2.indexOf(cPlantCode) != -1) {
                return tmpPlantCodes2;
            }
        }
        if (tmpPlantCodes.indexOf(cPlantCode) != -1) {
            return tmpPlantCodes;
        }
        return plantCodes;
    }
    
    private int isPlantCodeMatching(String plantCodes, String fnSerialIdent, String serialIdent, String abbr, String whcForChassis) {
        String formattedIdent = null;
        String serialPlantCodeGroup = null;
        String footnotePlantCodeGroup = null;
        int valid = 0;
        String serialIdentToCompare = null;
        String footnoteIdentToCompare = null;
        if (plantCodes == null || plantCodes.trim().equals("")) {
            return 0;
        }
        if (plantCodes.indexOf(serialIdent.charAt(1)) != -1) {
            serialPlantCodeGroup = this.getPlantCodeGroup(plantCodes, serialIdent.charAt(1));
        }
        formattedIdent = this.checkNewFTNoteFormat(fnSerialIdent);
        if (formattedIdent.length() == 0) {
            return 0;
        }
        if (formattedIdent.length() >= 7) {
            char cPlantCode;
            if (formattedIdent.length() == 7) {
                cPlantCode = formattedIdent.charAt(0);
            }
            else {
                cPlantCode = formattedIdent.charAt(1);
            }
            if (serialPlantCodeGroup == null || serialPlantCodeGroup.indexOf(cPlantCode) == -1) {
                return 0;
            }
            footnotePlantCodeGroup = this.getPlantCodeGroup(plantCodes, cPlantCode);
            if (!footnotePlantCodeGroup.equals(serialPlantCodeGroup)) {
                return 3;
            }
            if (this.whcForFootnote != null && !this.whcForFootnote.trim().equals("") && whcForChassis != null && !whcForChassis.trim().equals("") && !this.whcForFootnote.equals(whcForChassis)) {
                return 5;
            }
            int res = -1;
            res = serialPlantCodeGroup.indexOf(serialIdent.charAt(1));
            String millionthString;
            if (res >= 10) {
                millionthString = "" + res;
            }
            else {
                millionthString = "0" + res;
            }
            serialIdentToCompare = millionthString + serialIdent.substring(2, 8);
            res = footnotePlantCodeGroup.indexOf(cPlantCode);
            if (res >= 10) {
                millionthString = "" + res;
            }
            else {
                millionthString = "0" + res;
            }
            if (formattedIdent.length() == 7) {
                footnoteIdentToCompare = millionthString + formattedIdent.substring(1, 7);
            }
            else {
                footnoteIdentToCompare = millionthString + formattedIdent.substring(2, 8);
            }
        }
        if (abbr.equals("#BF")) {
            if (serialIdentToCompare.compareTo(footnoteIdentToCompare) > 0) {
                valid = 1;
            }
            else {
                valid = 8;
            }
        }
        else if (abbr.equals("#AF")) {
            if (serialIdentToCompare.compareTo(footnoteIdentToCompare) < 0) {
                valid = 1;
            }
            else {
                valid = 8;
            }
        }
        return valid;
    }
    
    private int processChassis(FootnoteList fnList, String chassis, String fnSerialNumber, String whc, String abbr) {
        int valid = 0;
        String plantCodes = fnList.getPlants();
        if (fnSerialNumber.length() == 6 || (fnSerialNumber.length() == 7 && Character.isDigit(fnSerialNumber.charAt(0)) && this.isSixNums(fnSerialNumber.substring(1, 7))) || (fnSerialNumber.length() == 8 && Character.isDigit(fnSerialNumber.charAt(0)) && Character.isDigit(fnSerialNumber.charAt(1)) && this.isSixNums(fnSerialNumber.substring(2, 8)))) {
            String formattedChassis = chassis.substring(2, 8);
            String formattedIdent = this.checkNewFTNoteFormat(fnSerialNumber);
            if (this.whcForFootnote != null && !this.whcForFootnote.trim().equals("") && whc != null && !whc.trim().equals("") && !this.whcForFootnote.equals(whc)) {
                return 5;
            }
            if (abbr.equals("#AF")) {
                if (formattedChassis.compareTo(formattedIdent) < 0) {
                    valid = 1;
                }
                else {
                    valid = 8;
                }
            }
            else if (abbr.equals("#BF")) {
                if (formattedChassis.compareTo(formattedIdent) > 0) {
                    valid = 1;
                }
                else {
                    valid = 8;
                }
            }
        }
        else {
            if (this.whcForFootnote != null && !this.whcForFootnote.trim().equals("") && whc != null && !whc.trim().equals("") && !this.whcForFootnote.equals(whc)) {
                return 5;
            }
            valid = this.isPlantCodeMatching(plantCodes, fnSerialNumber, chassis, abbr, whc);
        }
        return valid;
    }
    
    private int processMotor(String engine, String fnSerialNumber, String abbr) {
        int valid = 0;
        try {
            if (engine == null || engine.trim().length() == 0) {
                return valid;
            }
            if (fnSerialNumber.length() < 8) {
                engine = BenzStringUtil.removeBlank(engine);
                if (engine.length() > 6) {
                    engine = engine.substring(engine.length() - 6);
                }
                fnSerialNumber = BenzStringUtil.removeBlank(fnSerialNumber);
                int engineNum = Integer.parseInt(engine);
                int serialNum = Integer.parseInt(fnSerialNumber);
                if (abbr.equals("#AM")) {
                    if (engineNum < serialNum) {
                        valid = 1;
                    }
                    else {
                        valid = 8;
                    }
                }
                if (abbr.equals("#BM")) {
                    if (engineNum > serialNum) {
                        valid = 1;
                    }
                    else {
                        valid = 8;
                    }
                }
            }
            else {
                engine = BenzStringUtil.removeBlank(engine);
                fnSerialNumber = BenzStringUtil.removeBlank(fnSerialNumber);
                if (engine.length() >= 13) {
                    if (fnSerialNumber.startsWith("**")) {
                        fnSerialNumber = fnSerialNumber.substring(2);
                    }
                    else if (fnSerialNumber.startsWith("*")) {
                        fnSerialNumber = fnSerialNumber.substring(1);
                    }
                    if (fnSerialNumber.length() == 8) {
                        if (!fnSerialNumber.startsWith(engine.substring(6, 7))) {
                            return 3;
                        }
                        fnSerialNumber = fnSerialNumber.substring(1);
                    }
                    int snLen = fnSerialNumber.length();
                    String engineserial = engine.substring(engine.length() - snLen);
                    int engineNum2 = Integer.parseInt(engineserial);
                    int serialNum2 = Integer.parseInt(fnSerialNumber);
                    if (abbr.equals("#AM")) {
                        if (engineNum2 >= serialNum2) {
                            valid = 8;
                        }
                        else {
                            valid = 1;
                        }
                    }
                    if (abbr.equals("#BM")) {
                        if (engineNum2 <= serialNum2) {
                            valid = 8;
                        }
                        else {
                            valid = 1;
                        }
                    }
                }
            }
        }
        catch (Exception e) {
        }
        return valid;
    }
    
    private int processCab(String cab, String fnSerialNumber, BenzVinInfoDTO benzVinInfoDTO, String abbr) {
        int valid = 0;
        try {
            if (cab == null || cab.trim().length() == 0) {
                return 0;
            }
            fnSerialNumber = BenzStringUtil.removeBlank(fnSerialNumber);
            if (fnSerialNumber.length() == 7 && fnSerialNumber.startsWith("0")) {
                fnSerialNumber = fnSerialNumber.substring(1);
            }
            cab = BenzStringUtil.removeBlank(cab).substring(6);
            if (benzVinInfoDTO != null && benzVinInfoDTO.getVehClass() != null) {
                if (benzVinInfoDTO.getVehClass().equals("2")) {
                    fnSerialNumber = fnSerialNumber.substring(1);
                    cab = cab.substring(2);
                }
                else if ("34567".indexOf(benzVinInfoDTO.getVehClass().charAt(0)) != -1) {
                    cab = cab.substring(cab.length() - 7);
                }
            }
            int cabNum = 0;
            if (fnSerialNumber.length() == 6) {
                cabNum = Integer.parseInt(cab.substring(1));
            }
            else {
                cabNum = Integer.parseInt(cab);
            }
            int serialNum = Integer.parseInt(fnSerialNumber);
            if (abbr.equals("#AA")) {
                if (cabNum >= serialNum) {
                    valid = 8;
                }
                else {
                    valid = 1;
                }
            }
            else if (abbr.equals("#BA")) {
                if (cabNum <= serialNum) {
                    valid = 8;
                }
                else {
                    valid = 1;
                }
            }
        }
        catch (Exception e) {
        }
        return valid;
    }
    
    private boolean isSixNums(String serialIdent) {
        for (int i = 0; i < 6; ++i) {
            if (!Character.isDigit(serialIdent.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
